/**
 * Copyright (C) 2008 Christopher Allen Ogden
 *
 *     This file is part of Odysi.
 *
 *  Odysi is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Odysi is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Odysi.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "mobile.h"

void Mobile::tick(void)
{
    Object::tick();
    Location loc(getLocation());
    if(_moving)
    {
        float total = fabs(_xOffGoal) + fabs(_yOffGoal);
        float xDiff = _xOffGoal / total * _speed;
        float yDiff = _yOffGoal / total * _speed;
        float zDiff = _zOffGoal / total * _speed;
        if(fabs(_xOffGoal) - fabs(xDiff) < 0) xDiff = _xOffGoal;
        if(fabs(_yOffGoal) - fabs(yDiff) < 0) yDiff = _yOffGoal;
        if(fabs(_zOffGoal) - fabs(zDiff) < 0) zDiff = _zOffGoal;
        _xOffGoal -= xDiff;
        _yOffGoal -= yDiff;
        int zDiffi = (zDiff > 0) ? static_cast<int>(zDiff + 0.5f) : static_cast<int>(zDiff - 0.5f);
        _zOffGoal -= zDiffi;
        if(_xOffGoal == 0 && _yOffGoal == 0 && _zOffGoal == 0)
        {
            _moving = false;
        }
        setXOffset(loc.getXOff() + xDiff);
        setYOffset(loc.getYOff() + yDiff);
        setZ(loc.getZ() + zDiffi);
        if(getX() > _maxX)
            setX(0);
        if(getY() > _maxY)
            setY(0);
        if(getX() < 0)
            setX(_maxX);
        if(getY() < 0)
            setY(_maxY);
        if(getZ() < 0)
            setZ(0);
    }
}
